<?php
/**
 * <pre>
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT      : rubik-base
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * AbstractHtmlFilter lớp Abstract dùng để lớp con HTMLFilter extends.
 * Cấu trúc của lớp con: NewsHtmlFilter extends AbstractHtmlFilter
 * 	các public properties sẽ được dùng để tạo ra filter box
 * 
 * Cú pháp của lớp properties lớp con:
 * 		var $<giá trị> = "<Tên cần hiển thị trong combobox>";
 *
 * </pre>
 * 
 * @version 		1.0
 * @author			Dang Nhon Hoa <hoadn@rubik.com.vn>
 * @access			public
 * @package			rubik.base.html
 *
 * Created on	: May 24, 2008
 */

class AbstractHtmlCombobox {
	
	/**
	 * Name of current combobox
	 *
	 * @var string
	 */
	var $_cbxName = null;
	
	/**
	 * Value of current combobox
	 *
	 * @var string
	 */
	var $_selectedValue = null;
	
	/**
	 * Flag indicate the CBX is multiple select or not.
	 *
	 * @var boolean
	 */
	var $_isMultipleSelect = false;
	
	/**
	 * The array for containing data
	 *
	 * @var array
	 */ 
	var $_arrayData;
	
	/**
	 * Constructor
	 *
	 * @param string $cbxName the name of COMBOBOX control will ben generate
	 * @param string/array $valueOrArrayContain this array normaly is $_POST or $_GET or $_REQUEST
	 */
	function __construct($cbxName, $valueOrArrayContain = null) {
		$this->_cbxName = $cbxName;
		$this->setValue ( $valueOrArrayContain );
	}
	
	/**
	 * Multiple select
	 *
	 * @return boolean
	 */
	function isMultipleSelect() {
		return $this->_isMultipleSelect;
	}
	
	/**
	 * Multiple select
	 *
	 * @return boolean
	 */
	function setMultipleSelect($bool = true) {
		$this->_isMultipleSelect = $bool;
	}
	
	/**
	 * Set the value for this control from the array contain the value
	 *
	 * @param string/array $valueOrArrayContain this array normaly is $_POST or $_GET or $_REQUEST
	 */
	function setValue($valueOrArrayContain) {
		if (empty($this->_selectedValue) && $this->_isMultipleSelect && is_array($valueOrArrayContain)) {
			$this->_selectedValue = $valueOrArrayContain;
		}
		else if (is_string($valueOrArrayContain)) {
			$this->_selectedValue = $valueOrArrayContain;
		}
		else {
			$this->_selectedValue = RubikHelper::getValueFrom($valueOrArrayContain, $this->_cbxName);
		}
		return true;
	}
	
	/**
	 * Get the value of combobox
	 *
	 * @return string
	 */
	function getSelectedValue() {
		return $this->_selectedValue;
	}
	
	/**
	 * Create the hidden field for this 
	 * This function should be used in case the doesn't appeare 
	 * but we need it when we click back
	 *
	 * @param string $cbxId
	 * @return string
	 */
	function createHiddenHtml($cbxId = null) {
		if ($cbxId === null) {
			$cbxId = $this->_cbxName;
		}
		
		$result = "<input type=\"hidden\" id=\"$cbxId\" name=\"{$this->_cbxName}\" value=\"{$this->_selectedValue}\" />\n";
		
		return $result;
	}
	
	/**
	 * Create HTML text for the combobox
	 *
	 * @param string $emptyText the text for empty value. if this equal to null then there isn't any blank value in combobox
	 * @param string $cssCls
	 * @param string $style
	 * @param string $id
	 * @param string $extAttributes
	 * @return string
	 */
	function createCombobox($emptyText = "- - Select Filter - -", $cssCls = null, 
								$style = null, $id = null, $extAttributes = "") {
		$inputName = $this->_cbxName;
		if ($this->_isMultipleSelect) {
			$emptyText = null;
		}
		
		if ($emptyText !== null) {
			$fields [""] = $emptyText;
		}
		
		if (empty($this->_arrayData)) {
			$vars = get_object_vars ( $this );
			foreach ( $vars as $key => $value ) {
				if ('_' == substr ( $key, 0, 1 )) {
					continue;
				}
				$fields [$key] = $value;
			}
			$this->_arrayData = &$fields;
		}
		
		RCoreImporter::cls("helper.RHtmlHelper" );
		return RHtmlHelper::createSelectHtml ($inputName , $this->_arrayData, $this->_selectedValue, 
											$extAttributes, $id, $cssCls, $style, $this->_isMultipleSelect, false);
	}
	
	/**
	 * Combobox
	 *
	 * @return string
	 */
	function getCbxName() {
		return $this->_cbxName;
	}
}
?>